package com.thd.springboottest.core.a.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller

public class NormalController {
    @RequestMapping("/tt")
    @ResponseBody
    public String test(HttpServletResponse response){
        System.out.println("tt success");
        Cookie cookie = new Cookie("aaa", "bbb");
        cookie.setMaxAge(30* 60);// 设置为30min
        cookie.setPath("/");
        System.out.println("已添加===============");
        response.addCookie(cookie);

        return "tt success";
    }

    @RequestMapping("/showCookie")
    @ResponseBody
    public String showCookie(HttpServletResponse response, HttpServletRequest request){
        System.out.println("showCookie ");

        Cookie[] cookies = request.getCookies();//这样便可以获取一个cookie数组
        String cookieStr = "";
        if(null==cookies) {
            cookieStr = "----";
        }else{
            for(Cookie cookie : cookies){
                cookieStr+=("name:"+cookie.getName()+",value:"+ cookie.getValue());
            }
        }

        return cookieStr;
    }
}
